<template>
  <editForm
    :formConfig="basicConfig"
    ref="basicForm"
    :formData="formData"
  ></editForm>
</template>

<script>
export default {
  props: {
    allInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    status: {
      type: Number,
    },
  },
  data() {
    return {
      minTime: null,
      maxTime: null,
      basicConfig: {
        labelWidth: "160px",
        itemArr: [
          {
            label: "实际开始时间",
            prop: "factStartTime",
            type: "datetime",
            required: true,
            disabled: false,
            span: 8,
            rules: {
              trigger: "blur",
              validator: (rule, value, callback) => {
                this.checkTime(rule, value, callback);
              },
            },
            pickerOptions: {
              disabledDate: (time) => {
                return (
                  time.getTime() < this.minTime ||
                  time.getTime() >= this.maxTime
                );
              },
            },
          },
          {
            label: "备注人",
            prop: "startCheckerName",
            type: "text",
            required: false,
            disabled: true,
            span: 8,
          },
          {
            label: "备注内容",
            prop: "startDesc",
            type: "textarea",
            required: true,
            span: 24,
            rows: 4,
          },
        ],
      },
      formData: {
        startCheckerName: "",
      },
    };
  },
  created() {
    if (this.status > 2) {
      this.basicConfig.itemArr.map((i) => (i.disabled = true));
    }
    //this.init(this.allInfo);
  },
  watch: {
    allInfo: {
      handler(val) {
        this.init(val);
      },
      deep: true,
    },
    status: {
      handler(val) {
        if (val >2 ) {
          this.basicConfig.itemArr.map((i) => (i.disabled = true));
        }
      },
    },
  },
  methods: {
    //初始化
    init(val) {
      if (val.applicationTime) {
        this.formData = JSON.parse(JSON.stringify(val.applicationTime));
      } 
      if (val.applicationCheck) {
        this.minTime = new Date(
          `${val.applicationCheck.startTime.split(" ")[0]} 00:00`
        ).getTime();
        this.maxTime = new Date(
          `${val.applicationCheck.endTime.split(" ")[0]} 24:00`
        ).getTime();
      }
    },
    //校验申请时间
    checkTime(rule, val, callback) {
      let minTime = new Date(this.allInfo.applicationCheck.startTime).getTime();
      let maxTime = new Date(this.allInfo.applicationCheck.endTime).getTime();
      let time = new Date(val).getTime();
      let message = `请在${this.allInfo.applicationCheck.startTime}-${this.allInfo.applicationCheck.endTime}内选择`;
      if (minTime > time || time > maxTime) {
        return callback(new Error(message));
      } else {
        callback();
      }
    },
    //获取实际开始数据
    async getFactStartData() {
      let formData = await this.$refs.basicForm.getFormData();
      if (!formData.startCheckerName) {
        formData.startCheckerName = this.$store.getters.userInfo.name;
      }
      return formData;
    },
  },
};
</script>

<style lang="less" scoped>
</style>